package de.h_da.fbi.cg.pointcloud.parser.ply.conf;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Scanner;

import org.anddev.progressmonitor.ProgressMonitor;

import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;

import de.h_da.fbi.cg.pointcloud.parser.ply.adt.PLYConfigTransformation;
import de.h_da.fbi.cg.pointcloud.util.StreamUtils;

/**
 * @author Nicolas Gramlich
 * @since 23:08:03 - 27.10.2010
 */
public class SimplePLYConfigParser {
	// ===========================================================
	// Constants
	// ===========================================================

	// ===========================================================
	// Fields
	// ===========================================================

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	// ===========================================================
	// Methods
	// ===========================================================


	public static HashMap<String, PLYConfigTransformation> parse(final InputStream pInputStream, final ProgressMonitor pProgressMonitor) throws IOException {
		pProgressMonitor.onProgressChanged(0);

		final Scanner scanner = new Scanner(pInputStream).useLocale(Locale.US);
		try {

			final HashMap<String, PLYConfigTransformation> out = new HashMap<String, PLYConfigTransformation>();
			while(scanner.hasNextLine() && scanner.hasNext()) {
				final String token = scanner.next();

				if(token.equals("bmesh")) {
					final String filename = scanner.next();
					/* Translation. */
					final float tx = scanner.nextFloat();
					final float ty = scanner.nextFloat();
					final float tz = -scanner.nextFloat();

					/* Quaternion. */
					final float qw = -scanner.nextFloat();
					final float qx = scanner.nextFloat();
					final float qy = scanner.nextFloat();
					final float qz = scanner.nextFloat();

					out.put(filename, new PLYConfigTransformation(new Vector3f(tx, ty, tz), new Quaternion(qx, qy, qz, qw)));
				} else {
					scanner.nextLine();
				}
			}

			pProgressMonitor.onProgressChanged(100);

			return out;
		} finally {
			StreamUtils.closeStream(pInputStream);
		}
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
